<template>
  <div class="app-container">
    <!-- 搜索框 -->
    <div class="search-header">
      <el-input 
        placeholder="搜索课程..." 
        v-model="searchText" 
        suffix-icon="el-icon-search" 
        @keyup.enter.native="handleSearch"
      ></el-input>
      <el-button type="primary" @click="handleSearch">搜索</el-button>
    </div>

    <!-- 搜索结果轮播图 -->
    <div v-if="searchResults.length > 0">
      <el-carousel height="400px" indicator-position="outside">
        <el-carousel-item v-for="item in searchResults" :key="item.id">
          <img :src="item.imageUrl" alt="课程轮播" class="carousel-img">
          <div class="carousel-caption">
            <h3>{{ item.title }}</h3>
            <p>{{ item.description }}</p>
            <el-button type="primary" @click="goToCourseDetail(item.courseId)">查看课程</el-button>
          </div>
        </el-carousel-item>
      </el-carousel>
    </div>
    <div v-else class="no-results">
      <p>暂无搜索结果</p>
    </div>
  </div>
</template>

<script>
export default {
  name: 'SearchPage',
  data() {
    return {
      searchText: this.$route.query.keyword || '',
      searchResults: this.$route.query.results ? JSON.parse(this.$route.query.results) : []
    }
  },
  methods: {
    handleSearch() {
      if (!this.searchText.trim()) {
        this.$message.warning('请输入搜索内容');
        return;
      }
      this.$router.push({
        path: '/students/search',
        query: {
          keyword: this.searchText
        }
      });
      // 这里应该调用搜索API，示例中直接使用路由传递的结果
    },
    goToCourseDetail(courseId) {
      this.$router.push(`/course/detail/${courseId}`);
    }
  }
}
</script>

<style scoped>
.search-header {
  display: flex;
  margin: 20px 0;
  max-width: 500px;
}

.search-header .el-input {
  margin-right: 10px;
}

.no-results {
  text-align: center;
  padding: 50px;
  font-size: 16px;
  color: #666;
}

/* 复用Index_stu.vue中的轮播图样式 */
.el-carousel {
  margin-bottom: 30px;
}

.carousel-img {
  width: 100%;
  height: 400px;
  object-fit: cover;
}

.carousel-caption {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 20px;
  background: linear-gradient(transparent, rgba(0, 0, 0, 0.7));
  color: white;
}

.carousel-caption h3 {
  font-size: 24px;
  margin-bottom: 10px;
}

.carousel-caption p {
  font-size: 16px;
  margin-bottom: 15px;
}
</style>